package com.socket.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUitl {
    private static String pattern = "yyyy-MM-dd HH:mm:ss SSS";
    private static String source = "2022-08-11 23:24:00 000";

    /**
     * @param pattern
     * @param source
     * @return
     * @将字符串转换成时间格式
     */
    public static Date getDate(String pattern, String source) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String source) {
        return getDate(pattern, source);
    }

    public static Date getDate() {
        return getDate(source);
    }

    /**
     * @param pattern
     * @return
     * @将时间按格式转换成字符串
     */
    public static String getDataStr(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String getDataStr(String pattern) {
        return getDataStr(new Date(), pattern);
    }

    public static String getDataStr() {
        return getDataStr(pattern);
    }
}
